package Jcat.cduestcbbs.DaoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jcat.hbc.connectionpool.HBCConnectionPool;
import Jcat.cduestcbbs.Dao.ReplyDao;
import Jcat.cduestcbbs.Po.PostsPo;
import Jcat.cduestcbbs.Po.ReplyPo;
import Jcat.cduestcbbs.tools.PagePo;

import Jcat.cduestcbbs.Po.UserPo;

public class ReplyDaoImpl implements ReplyDao {

	public List<ReplyPo> getAllReplys(PagePo page, int replys_posts_id)
			throws SQLException {
		List<ReplyPo> list = new ArrayList<ReplyPo>();
		String sql = "select * from replys where replys_posts_id = ? limit ?,?";
		ReplyPo replypo = null;
		Connection conn = HBCConnectionPool.getInstance().getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			System.out.println(page.getBeginIndex());
			pstmt.setInt(1, replys_posts_id);
			pstmt.setInt(2, page.getBeginIndex());
			pstmt.setInt(3, page.getPagesize());

			rs = pstmt.executeQuery();

			while (rs.next()) {
				replypo = new ReplyPo();
				replypo.setReplyId(rs.getInt("replys_id"));
				replypo.setReplyContent(rs.getString("replys_content"));
				replypo.setReplyDate((rs.getDate("replys_time")));
				replypo.setReplyPostsId(rs.getInt("replys_posts_id"));
				replypo.setUser(findUserById(rs.getInt("replys_person_id")));
				list.add(replypo);
			}
		} finally {
			pstmt.close();
			rs.close();
			conn.close();
		}
		return list;
	}

	public int getAllReplysCount(int replys_posts_id) throws SQLException {
		int count = 0;
		String sql = "select count(*) from replys where replys_posts_id = ?";
		Connection conn = HBCConnectionPool.getInstance().getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, replys_posts_id);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			pstmt.close();
			rs.close();
			conn.close();
		}
		return count;
	}

	public PostsPo findPostPoById(int id) throws SQLException {
		PostsPo postspo = null;
		String sql = "select * from posts where posts_id = ?";
		Connection conn = HBCConnectionPool.getInstance().getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);

			rs = pstmt.executeQuery();
			while (rs.next()) {
				postspo = new PostsPo();
				postspo.setPostsTitle(rs.getString("posts_title"));
				postspo.setPostsId(rs.getInt("posts_id"));
				postspo.setPostsCreateTime(rs.getDate("posts_create_time"));
				postspo.setPostsLook(rs.getInt("posts_looked"));
				postspo.setPostsType(rs.getString("posts_type"));
				postspo.setPostsContent(rs.getString("posts_content"));
				postspo.setUserPo(findUserById(rs.getInt("posts_author_id")));
			}
		} finally {
			pstmt.close();
			rs.close();
			conn.close();
		}
		return postspo;
	}

	public UserPo findUserById(int id) throws SQLException {
		UserPo userpo = null;
		String sql = "select * from person where person_id = ?";
		Connection conn = HBCConnectionPool.getInstance().getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);

			rs = pstmt.executeQuery();
			while (rs.next()) {
				userpo = new UserPo();
				userpo.setUserId(id);
				userpo.setUserName(rs.getString("person_name"));
			}
		} finally {
			pstmt.close();
			rs.close();
			conn.close();
		}
		return userpo;
	}

	public boolean deleteReply(ReplyPo reply) throws SQLException {
		return false;
	}

	public boolean insertReply(ReplyPo reply) throws SQLException {
		try {
			Connection con = HBCConnectionPool.getInstance().getConnection();
			Statement st = null;
			st = con.createStatement();

			if (st
					.executeUpdate("insert into replys (replys_content,replys_time) values ("

							+ reply.getReplyContent()
							+ ","
							+ reply.getReplyDate() + ");") == 1) {
				con.close();
				return true;
			} else {
				con.close();
				return false;
			}

		} catch (SQLException e) {
			return false;
		}
	}

	public List<ReplyPo> replyQuery() throws SQLException {
		Connection con = HBCConnectionPool.getInstance().getConnection();
		List<ReplyPo> list = new ArrayList<ReplyPo>();
		Statement st = null;
		ResultSet rs = null;
		st = con.createStatement();
		rs = st
				.executeQuery("select * from replys order by replys_id DESC limit 0,10");
		while (rs.next()) {

			ReplyPo reply = new ReplyPo();
			reply.setReplyId(Integer.parseInt(rs.getObject("replys_id")
					.toString()));
			list.add(reply);
		}

		return list;
	}

	public Map<Integer, Integer> getPostsPagingMap() throws SQLException {
		Connection con = HBCConnectionPool.getInstance().getConnection();
		Map<Integer, Integer> PagingMap = new HashMap<Integer, Integer>();
		Statement st = null;
		ResultSet rs = null;
		st = con.createStatement();
		rs = st.executeQuery("select replys_id,replys_posts_id from replys");
		while (rs.next()) {
			if (PagingMap.containsKey(rs.getInt(2))) {
				PagingMap.put(rs.getInt(2), PagingMap.get(rs.getInt(2)) + 1);

			} else {
				PagingMap.put(rs.getInt(2), 1);

			}

		}

		return PagingMap;
	}
	
	public boolean deleteRely(int replys_id) throws SQLException{
		boolean flag = false;
		String sql = "delete from replys where replys_id=?";
		Connection conn = HBCConnectionPool.getInstance().getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, replys_id);
			int count = pstmt.executeUpdate();
			if(count>0){
				flag = true;
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			pstmt.close();
			conn.close();
		}
        return flag;
	}
	
	public boolean deleteRelys(int replys_posts_id) throws SQLException{
		boolean flag = false;
		String sql = "delete from replys where replys_posts_id=?";
		Connection conn = HBCConnectionPool.getInstance().getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, replys_posts_id);
			int count = pstmt.executeUpdate();
			if(count>0){
				flag = true;
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			pstmt.close();
			conn.close();
		}
        return flag;
	}


}
